package com.chuanjiabao.wisdom.ui.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;

import com.chuanjiabao.wisdom.MainActivity;
import com.chuanjiabao.wisdom.R;
import com.chuanjiabao.wisdom.adapter.UpdateFileAdapter;
import com.chuanjiabao.wisdom.application.FApplication;
import com.chuanjiabao.wisdom.bean.RefreshMessageEvent;
import com.chuanjiabao.wisdom.bean.UpdateFileTeam;
import com.chuanjiabao.wisdom.databinding.ActivityUpdateFileBinding;
import com.chuanjiabao.wisdom.greendao.gen.UpdateFileTeamDao;
import com.chuanjiabao.wisdom.listener.NetworkCallbackImpl;
import com.chuanjiabao.wisdom.ui.ActivityStack;
import com.chuanjiabao.wisdom.ui.BaseActivity;
import com.chuanjiabao.wisdom.util.SpResourceUtil;
import com.chuanjiabao.wisdom.util.SpUtils;
import com.chuanjiabao.wisdom.util.ToolUtil;
import com.chuanjiabao.wisdom.util.dialog.ArriveDialog;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class UpdateFileActivity extends BaseActivity {

    private ActivityUpdateFileBinding binding;
    private UpdateFileAdapter adapter;
    private Disposable disposable;

    private List<UpdateFileTeam> list = new ArrayList<>();
    private List<UpdateFileTeam> listSecond = new ArrayList<>();
    private UpdateFileTeamDao teamDao = FApplication.getInstance().getDaoSession().getUpdateFileTeamDao();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_update_file);
        binding.setActivity(this);
        initView();

        disposable = Flowable.interval(1000, TimeUnit.MILLISECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(aLong -> {
                    listSecond = teamDao.loadAll();
                    if (listSecond.isEmpty()) {
                        list.clear();
                        adapter.notifyDataSetChanged();
                    } else {
                        list.clear();
                        list.addAll(listSecond);
                        adapter.notifyDataSetChanged();
                    }
                });
    }

    private void initView() {
        adapter = new UpdateFileAdapter(this, list);
        binding.recycler.setLayoutManager(new LinearLayoutManager(this));
        binding.recycler.setAdapter(adapter);

        if (TextUtils.equals("1", SpResourceUtil.getWifiStatus())) {
            binding.tvModuleHint.setText("已开启");
            binding.ivModuleChange.setImageResource(R.drawable.icon_pwd_open);
        } else {
            binding.tvModuleHint.setText("未开启");
            binding.ivModuleChange.setImageResource(R.drawable.icon_pwd_close);
        }

        binding.rlModuleLeft.setOnClickListener(this);
        binding.ivModuleMenu.setOnClickListener(this);
        binding.ivModuleChange.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ivModuleMenu:
                ArriveDialog arriveDialog = new ArriveDialog(this);
                arriveDialog.show();
                arriveDialog.setOnBHClickListener(position -> {
                    RefreshMessageEvent event = new RefreshMessageEvent();
                    event.setType("main");
                    event.setId(position + "");
                    EventBus.getDefault().post(event);
                    ActivityStack.getScreenManager().popAllActivityExceptOne(MainActivity.class);
                });
                break;
            case R.id.rlModuleLeft:
                finish();
                break;
            case R.id.ivModuleChange:
                if (TextUtils.equals("1", SpResourceUtil.getWifiStatus())) {
                    binding.tvModuleHint.setText("未开启");
                    binding.ivModuleChange.setImageResource(R.drawable.icon_pwd_close);
                    SpUtils.put("module_wifi", "0");
                } else {
                    binding.tvModuleHint.setText("已开启");
                    binding.ivModuleChange.setImageResource(R.drawable.icon_pwd_open);
                    SpUtils.put("module_wifi", "1");

                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                        boolean isWifi =
                                TextUtils.equals(
                                        "wifi",
                                        NetworkCallbackImpl.networkState.getValue()
                                );
                        if (!list.isEmpty() && !isWifi) {
                            Intent intentDelete = new Intent();
                            intentDelete.setAction("MODULE_ABOUT_RELATION");
                            intentDelete.putExtra("module_funcation", "5");
                            sendBroadcast(intentDelete);
                        }
                    } else {
                        boolean isWifi = ToolUtil.isWifiConnected(this);
                        if (!list.isEmpty() && !isWifi) {
                            Intent intentDelete = new Intent();
                            intentDelete.setAction("MODULE_ABOUT_RELATION");
                            intentDelete.putExtra("module_funcation", "5");
                            sendBroadcast(intentDelete);
                        }
                    }
                }
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (disposable != null) disposable.dispose();
    }

}
